package me.zhengjie.modules.equipment.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.equipment.domain.TSparepartRetrieval;
import me.zhengjie.modules.equipment.service.TSparepartRetrievalService;
import me.zhengjie.modules.equipment.service.TSparepartService;
import me.zhengjie.modules.equipment.service.dto.TSparepartDTO;
import me.zhengjie.modules.equipment.service.dto.TSparepartRetrievalQueryCriteria;
import me.zhengjie.utils.HttpResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
* @author jie
* @date 2019-11-16
*/
@RestController
@RequestMapping("api")
public class TSparepartRetrievalController {

    @Autowired
    private TSparepartRetrievalService tSparepartRetrievalService;

    @Autowired
    private TSparepartService tSparepartService;

    @Log("查询TSparepartRetrieval")
    @GetMapping(value = "/tSparepartRetrieval")
    @PreAuthorize("hasAnyRole('ADMIN','TSPAREPARTRETRIEVAL_ALL','TSPAREPARTRETRIEVAL_SELECT')")
    public ResponseEntity getTSparepartRetrievals(TSparepartRetrievalQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(tSparepartRetrievalService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增TSparepartRetrieval")
    @PostMapping(value = "/tSparepartRetrieval")
    @PreAuthorize("hasAnyRole('ADMIN','TSPAREPARTRETRIEVAL_ALL','TSPAREPARTRETRIEVAL_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TSparepartRetrieval resources){
        try{
            TSparepartDTO sparepartDTO = tSparepartService.findById(resources.getEqptId());
            if(sparepartDTO == null)
                return HttpResponseUtils.passDefaultResponse("备件信息不存在!", HttpStatus.BAD_REQUEST);

            if(resources.getState().equals(sparepartDTO.getState())) {
                return HttpResponseUtils.passDefaultResponse("状态相同,请勿重复操作", HttpStatus.BAD_REQUEST);
            }
            return new ResponseEntity(tSparepartRetrievalService.log(resources),HttpStatus.CREATED);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }

    @Log("修改TSparepartRetrieval")
    @PutMapping(value = "/tSparepartRetrieval")
    @PreAuthorize("hasAnyRole('ADMIN','TSPAREPARTRETRIEVAL_ALL','TSPAREPARTRETRIEVAL_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TSparepartRetrieval resources){
        tSparepartRetrievalService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除TSparepartRetrieval")
    @DeleteMapping(value = "/tSparepartRetrieval/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','TSPAREPARTRETRIEVAL_ALL','TSPAREPARTRETRIEVAL_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        tSparepartRetrievalService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}